/*
 * CADI Software - a JPIP Client/Server framework
 * Copyright (C) 2007  Group on Interactive Coding of Images (GICI)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Group on Interactive Coding of Images (GICI)
 * Department of Information and Communication Engineering
 * Autonomous University of Barcelona
 * 08193 - Bellaterra - Cerdanyola del Valles (Barcelona)
 * Spain
 *
 * http://gici.uab.es
 * gici-info@deic.uab.es
 */
package org.vast.jpip.message;

import java.io.PrintStream;

/**
 * This class is used to store the target fields.
 * <p>
 * Further information, see ISO/IEC 15444-9 section C.2
 *
 * @author Group on Interactive Coding of Images (GICI)
 * @version 1.0   2007/10/26   
 */
public class TargetField {

	/**  
	 * target = "target" "=" PATH
	 * 
	 * Specifies the original named resource (often the name of a file on the server).
	 * <p>
	 * If the Target request field is missing then the original named resource is determined by other means.
	 */
	public String target = null;	
	
	/**
	 * subtarget = "subtarget" "=" byte-range <br>
	 * byte-range = UINT_RANGE
	 * <p> 
	 * May be used to qualify the original named resource through the
	 * specification of a byte range. The logical target is to be interpreted as
	 * the indicated byte range of the original named resource.
	 * <p>
	 * The lower and upper bounds of the supplied byte-range are inclusive, an 0
	 * refers to the first byte of the target file.
	 */
	public int[] subtarget = null;
	
	/**
 	 * tid = "tid" "=" target-id <br>
	 * target-id = TOKEN
	 * <p> 
	 * This field may be used to supply a target-id string, which was previously generated by the server to absolutely identify
	 * the logical target that is being accessed, including any discretionary transcoding performed by the server. The
	 * logical target name is not necessarily unique and does not necessarily correspond to a single encoding of its contents,
	 * whereas the target-id string, together with the original resource name and byte range, should absolutely identify
	 * both the imagery and its encoding.
	 * <p>
	 * If target-id is "0", the logical target is specified through the use of the Target, Sub-target and JPIP URL path
	 * component, and the client is explicitly requesting that the server inform it of the assigned target-id, if there is one.
	 * The server shall include a Target ID header in its response to all client requests with a target-id of "0".
	 * <p>
	 * target-id shall not exceed 255 characters in length.
	 */
	public String tid = null;
	
	
	/**************************************************************************/
	/**                         PRINCIPAL METHODS                            **/ 
	/**************************************************************************/
	
	/**
	 * Constructor.
	 */
	public TargetField() {
		target=null;
		subtarget = null; //subtarget = new int[2]; subtarget[0] = subtarget[1] = -1;
		tid = null;
	}
	
	/**
	 * Sets the attributes to its initial values.
	 */
	public void reset() {
		target=null;	
		subtarget = null; // subtarget[0] = -1; subtarget[1] = -1;
		tid = null;;
	}

	/**
	 * For debugging purposes. 
	 */
	public String toString() {
		String str = "";

		str = getClass().getName() + " [";

		if (target != null)  str += "target=" + target;
				
		if (subtarget != null) {
			if (subtarget[0] != -1 ) {
				str += ", subtarget= " + subtarget[0] +"-"+ subtarget[1];
			}
		}
		
		if (tid != null) str += ", tid=" + tid;

		str += "]";
		return str;
	}
	
	/**
	 * Prints this Target fields out to the specified output stream.
	 * This method is useful for debugging.
	 * 
	 * @param out an output stream.
	 */
	public void list(PrintStream out) {

		out.println("-- Target fields --");		
		
		if (target != null) out.println("target: " + target);
		
		if (subtarget != null) {
			if (subtarget[0] != -1 ) {
				out.println("subtarget: " + subtarget[0] +"-"+ subtarget[1]);
			}
		}
		
		if (tid != null) out.println("tid: " + tid);
						
		out.flush();
	}	
	
}
